package com.lb.shopguide.ui.view;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;

/**
 *
 */
public class GridListItemDecoration extends RecyclerView.ItemDecoration {
    private static final int DEFAULT_PADDING = 3;
    private static final int DEFAULT_COLUMN = 3;
    private int headerCount = 0;
    private int padding = DEFAULT_PADDING;
    private int columnNumber = DEFAULT_COLUMN;


    public GridListItemDecoration(float padding, int columnNumber, int headerCount) {
        this.padding = SizeUtils.dp2px(padding);
        this.columnNumber = columnNumber;
        this.headerCount = headerCount;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        // item position
        int position = parent.getChildAdapterPosition(view) - headerCount;
        if (position < 0) {
            return;
        }
        // item column
        int column = position % columnNumber;
        int row = position / columnNumber;

        if (row == 1) {
            outRect.top = padding;
            outRect.bottom = padding;
        }

    }
}
